package ModelLayer;

import java.util.ArrayList;
/**
 * Write a description of class AddressBook here.
 * 
 * @author (DM80 Group 5, Anders, Michael, Jesper, Lars og Kenni) 
 * @version (16/11-2012)
 */
public class AddressBook
{
    private ArrayList<Person> persons;
    
    private static AddressBook instance;
    /**
     * Constructor for objects of class AddressBook
     */
    private AddressBook()
    {
        persons = new ArrayList<Person>();
    }
    
    public static AddressBook getAddressBook()
    {
        if(instance == null)
        {
            instance = new AddressBook();
        }
        
        return instance;
    }
    
    public void addPerson(Person _person)
    {
        persons.add(_person);
    }
    
    public Person searchById(int _id)
    {
        Person retPerson = null;
        int index = 0;
        boolean found = false;
        
        while(index < persons.size() && !found)
        {
            Person currPerson = persons.get(index);
            if(currPerson.getId() == _id)
            {
                retPerson = currPerson;
                found = true;
            }
            else
            {
                index++;
            }
        }
        
        return retPerson;
    }
        
    public ArrayList<Person> getAllPersons()
    {
        return persons;
    }
    
    public void deletePerson(Person _person)
    {
        persons.remove(_person);
    }
}
